package com.superyee.commonlib.observer;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Looper;

import com.superyee.commonlib.logic.BaseLogic;
import com.superyee.commonlib.utils.AppLog;

import java.util.ArrayList;
import java.util.HashMap;

@SuppressLint("UseSparseArrays")
public class ObserverManager {

    private static volatile ObserverManager manager;
    private HashMap<Integer, ArrayList<UiObserver>> observers = new HashMap<Integer, ArrayList<UiObserver>>();

    private Handler handler = new Handler(Looper.getMainLooper());
    public static final int SUCCESS = 0;
    public static final int START = 1;
    public static final int FINISH = 2;
    public static final int FAILURE = 3;
    public static final int NONETWORK = 4;
    private static final String TAG = ObserverManager.class
            .getSimpleName();


    public void post(Runnable runnable) {
        handler.post(runnable);
    }

    public void postDelayed(Runnable runnable, int delayMillis) {
        handler.postDelayed(runnable, delayMillis);
    }

    /**
     * 单例
     *
     * @return ObserverManager对象
     */
    public static ObserverManager getInstence() {
        if (manager == null) {
            synchronized (ObserverManager.class) {
                if (manager == null) {
                    manager = new ObserverManager();
                }
            }
        }
        return manager;
    }

    /**
     * 添加一个观察者对象
     *
     * @param actionLogic 前16位 uiaction 后16位代表逻辑处理action   也可以只有uiaction但长度不能超过16位也就是65535
     * @param observer
     */
    public synchronized void addObserver(int actionLogic, UiObserver observer) {
        if (observer == null) {
            throw new NullPointerException();
        }
        ArrayList<UiObserver> observerList = observers.get(actionLogic);
        if (observerList == null) {
            observerList = new ArrayList<UiObserver>();
            observers.put(actionLogic, observerList);
        }
        if (!observerList.contains(observer)) {
            observerList.add(observer);
        }
        AppLog.printD(TAG, "addObserver：observerSize:" + observers.size() + "---action:"
                + actionLogic + "---observer:" + observer.toString());
    }

    /**
     * 删除指定的观察者对象
     *
     * @param action
     */
    public synchronized void deleteObserver(int action, UiObserver observer) {
        if (observers != null) {
            if (observers.containsKey(action)) {
                ArrayList<UiObserver> observerList = observers.get(action);
                if (observerList != null) {
                    observerList.remove(observer);
                }
                AppLog.printD(TAG, "deleteObserver:" + observers.size() + "---observer:" + observer);
                if (observerList == null || observerList.isEmpty()) {
                    observers.remove(action);
                }
            }
        }
    }

    /**
     * 清空所有的观察者对象
     */
    public void clear() {
        if (observers != null) {
            observers.clear();
        }
    }

    public void notifyUi(final int baseAction, final Object obj,
                         final int statusCode) {
        int uiAction = BaseLogic.getUiAction(baseAction);
        final ArrayList<UiObserver> observer = observers.get(uiAction);
        if (observer != null) {
            handler.post(new Runnable() {
                public void run() {
                    try {
                        for (UiObserver ob : observer) {
                            AppLog.printD(TAG, "notifyUi call : class: = " + ob.toString());
                            int logicAction = BaseLogic.getLogicAction(baseAction);
                            ob.onReceiverNotify(logicAction, obj, statusCode);
                        }
                    } catch (Exception e) {
                        AppLog.printE(TAG, "notifyUi  call : Exception = " + e.toString());
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}
